package pers.li.service;

import pers.li.dao.UserDao;
import pers.li.model.User;
import pers.li.model.VO.UserVO;
import pers.li.utils.BeanCopy;

import java.util.HashMap;
import java.util.Map;


public class UserService extends UserDao{
    private final UserDao userDao = new UserDao();

    public Map<String, Object> login(String userName, String password) {
        Map<String, Object> map = new HashMap<>();
        User user = userDao.getUserByUserName(userName);
        if (user != null) {
            String userPass = user.getUserPass();
            if (userPass.equals(password)) {
                map.put("login", true);
                UserVO userVO = new UserVO();
                BeanCopy.copy(user, userVO);
                map.put("user", userVO);
            } else
                map.put("login", false);
        } else
            map.put("login", false);
        return map;
    }

    public Map<String, Object> regUser(User user) {
        user.setUserRank(0);
        Map<String, Object> map = new HashMap<>();
        int id = userDao.insertUser(user);
        if (id != -1) {
            User userTag = userDao.getUserById(id);
            map.put("reg", true);
            UserVO userVO = new UserVO();
            BeanCopy.copy(user, userVO);
            map.put("user", userVO);
        } else
            map.put("reg", false);
        return map;
    }

}
